package net.lab1024.sa.admin.module.business.exam.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.exam.dao.CourseaDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CourseaEntity;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.CourseaAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.CourseaQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.CourseaUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.CourseaVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-05-22 15:29:15
 * @Copyright AKITA
 */

@Service
public class CourseaService {

    @Resource
    private CourseaDao courseaDao;

    /**
     * 分页查询
     */
    public PageResult<CourseaVO> queryPage(CourseaQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CourseaVO> list = courseaDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CourseaAddForm addForm) {
        CourseaEntity courseaEntity = SmartBeanUtil.copy(addForm, CourseaEntity.class);
        Integer lineNum = courseaDao.getMaxLineNum(addForm.getDocEntry());
        if (lineNum == null) {
            lineNum = 0;
        }
        courseaEntity.setLineNum(++lineNum);
        courseaDao.insert(courseaEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(CourseaUpdateForm updateForm) {
        CourseaEntity courseaEntity = SmartBeanUtil.copy(updateForm, CourseaEntity.class);
        courseaDao.update(courseaEntity,new LambdaQueryWrapper<CourseaEntity>().eq(CourseaEntity::getDocEntry,courseaEntity.getDocEntry()).eq(CourseaEntity::getLineNum,courseaEntity.getLineNum()));
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        courseaDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry){
            return ResponseDTO.ok();
        }

        courseaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }
}
